package com.jy.app.lib.view.uilistview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.jy.app.lib.R;
import com.jy.app.lib.adapter.CommonAdapter;
import com.jy.app.lib.utils.DensityUtil;

import java.util.List;

/**
 * 对下拉刷新的UIListview、UILoadingLayout进行数据和动作的简单封装
 * Created by jan on 2016/3/29.
 */
public class UILoadListView<T> extends LinearLayout {
    public UIListView mListView;
    public UILoadingLayout mLoadingLayout;

    private CommonAdapter<T> mAdapter;
    private BaseAdapter baseAdapter;
    private Handler mHandler;

    private OnLoadEventListener onLoadEventListener = null;
    private OnItemClickListener onItemClickListener;
    private int convertViewResID;
    private int mDividerColor;
    private int mDividerHeight;

    public UILoadListView(Handler handler,Context context) {
        this(context, null);
        this.mHandler = handler;
    }

    public UILoadListView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public UILoadListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttributes(context,attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_test_listview,this,true);
        mListView = (UIListView) view.findViewById(R.id.test_lv);
        mListView.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        mLoadingLayout = (UILoadingLayout) view.findViewById(R.id.uiloadingLayout);
        initView();
        initListener();
    }

    private void initAttributes(Context context, AttributeSet attrs) {
        TypedArray types = context.obtainStyledAttributes(attrs,
                R.styleable.UILoadListView);
        final int count = types.getIndexCount();
        for (int i = 0; i < count; ++i) {
            int attr = types.getIndex(i);
            if (attr == R.styleable.UILoadListView_uil_divider) {
                mDividerColor = types.getColor(attr, 0);
            } else if (attr == R.styleable.UILoadListView_uil_dividerHeight) {
                mDividerHeight = types.getDimensionPixelSize(attr, DensityUtil.dp2px(context,1));
            }
        }
        types.recycle();
    }

    private void initListener(){
        mListView.setCListViewListener(new UIListView.ICListViewListener() {
            @Override
            public void onRefresh() {
                if (onLoadEventListener != null) {
                    onLoadEventListener.onListRefresh();
                }
                //TODO 执行数据请求

            }

            @Override
            public void onLoadMore() {
                if (onLoadEventListener != null) {
                    onLoadEventListener.onListLoadMore();
                }
                //TODO 执行数据请求
            }
        });
        mLoadingLayout.setRefreashListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLoadEventListener != null) {
                    onLoadEventListener.onLoadingRefresh();
                }
            }
        });
        mLoadingLayout.setListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLoadEventListener != null) {
                    onLoadEventListener.onNoButtonClickRefresh();
                }
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(parent,view,position-1,id);
                }
            }
        });
    }

    private void initView(){
//        if(mDividerColor!=0){
            ColorDrawable cd = new ColorDrawable(mDividerColor);
            mListView.setDivider(cd);
//        }
//        if(mDividerHeight!=0){
            mListView.setDividerHeight(mDividerHeight);
//        }
    }

    public void setmAdapter(CommonAdapter<T> mAdapter){
        if(mListView!=null){
            this.mAdapter = mAdapter;
            mListView.setAdapter(mAdapter);
        }
    }
    public void setAdapter(BaseAdapter mAdapter){
        if(mListView!=null){
            this.mAdapter = null;
            this.baseAdapter = mAdapter;
            mListView.setAdapter(mAdapter);
        }
    }

    public void updateData(List<T> data){
        if(mAdapter!=null){
            mAdapter.refreshAdapter(data);
        }else if(baseAdapter!=null){
            baseAdapter.notifyDataSetChanged();
        }
    }


    public void notifyDataSetChanged(){
        if(mAdapter !=null){
            mAdapter.notifyDataSetChanged();
        }else if(baseAdapter!=null){
            baseAdapter.notifyDataSetChanged();
        }
    }

    public void setOnLoadEventListener(OnLoadEventListener listener) {
        this.onLoadEventListener = listener;
    }

    public void setStatus(int statusCode){
        if(mLoadingLayout!=null){
            mLoadingLayout.showStatus(statusCode);
        }
    }

    public void setErrorText(String error){
        if (mLoadingLayout!=null){
            mLoadingLayout.setErrorText(error);
        }
    }

    public void showMoreView(){
        mListView.setEnableFooter(true);
    }

    public void hideMoreView(){
        mListView.setEnableFooter(false);
    }

    public void enableRightMenu(){
        mListView.initSlideMode(UIListView.MOD_RIGHT);
    }

    /**
     * 隐藏ListView提示界面
     */
    public  void hideLoadingLayout(){
        setStatus(UILoadingLayout.S_ShowData);
    }

    public void loadWarnNoNet(){
        setStatus(UILoadingLayout.S_Net_UnConnect);
    }

    public void loadWarnNetLoading(){
        setStatus(UILoadingLayout.S_Net_Loading);
    }
    /**网络异常情况的加载提示*/
    public void loadWarnNetFail(){
        setStatus(UILoadingLayout.S_Net_Fail);
    }

    public void loadWarnNoResult(){
        setStatus(UILoadingLayout.S_No_Data_Fail);
    }

    public void loadWarnTimeout(){
        setStatus(UILoadingLayout.S_Net_TimeOut);
    }

    public void isLoadCompleted(){
        mListView.LoadCompleted();
    }

    public void stopLvRefresh(){
        mListView.stopRefresh();
    }

    public void stopLvRefreshError(){
        mListView.stopRefreshError();
    }

    public void stopLvLoadMore(){
        mListView.stopLoadMore();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * Interface definition for a callback to be invoked when an item in this
     * AdapterView has been clicked.
     */
    public interface OnItemClickListener {

        /**
         * Callback method to be invoked when an item in this AdapterView has
         * been clicked.
         * <p>
         * Implementers can call getItemAtPosition(position) if they need
         * to access the data associated with the selected item.
         *
         * @param parent The AdapterView where the click happened.
         * @param view The view within the AdapterView that was clicked (this
         *            will be a view provided by the adapter)
         * @param position The position of the view in the adapter.
         * @param id The row id of the item that was clicked.
         */
        void onItemClick(AdapterView<?> parent, View view, int position, long id);
    }

    public interface OnLoadEventListener{
        /**
         * 下拉刷新的事件监听
         */
        public void onListRefresh();

        /**
         * 上拉加载的事件监听
         */
        public void onListLoadMore();

        /**
         * 加载数据或访问网络失败提示重新刷新的事件监听
         */
        public void onLoadingRefresh();

        /**
         * 在点击屏幕重新加载和刷新的背景下的事件监听
         */
        public void onNoButtonClickRefresh();
    }

    public void setonTouchListener(OnTouchListener onTouchListener) {
        mListView.onTouchListener = onTouchListener;
    }
}
